//
//  VideoCellViewModel.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/17.
//  Copyright © 2020 lt. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa

//播放器状态
enum DYPlayerStatus {
    case none
    case pause
    case playing
}

class VideoCellViewModel {
    
    let trillEntity: BehaviorRelay<TrillEntity>
    
    let isLikedStatus:BehaviorRelay<Bool> = BehaviorRelay(value: false)
    
    let status: BehaviorRelay<DYPlayerStatus> = BehaviorRelay(value: .none)
    
    let dyVideoLikeCount : BehaviorRelay<Int> = BehaviorRelay(value: 0)
    
    let isCollectionStatus : BehaviorRelay<Bool> = BehaviorRelay(value: false)
    
    var bag: DisposeBag = DisposeBag()
    
    init(trillEntity: TrillEntity) {
        self.trillEntity = BehaviorRelay(value: trillEntity)
        self.dyVideoLikeCount.accept(Int(trillEntity.dyVideoLikeCount ?? "0") ?? 0)
    }
}


// MARK: - 界面数据绑定
extension VideoCellViewModel {
    
     var trillEntityObserver: Observable<TrillEntity> {
        return trillEntity.share().asObservable()
    }
    
     var trillEntityDriver: Driver<TrillEntity> {
        return trillEntity.asDriver()
    }
    
     var playUrl: Observable<URL?> {
        return trillEntityObserver.map { (trillEntity) -> URL? in
            guard let urlStr:String = trillEntity.dyVideoUrl else { return nil }
            let url = URL(string: urlStr)
            return url
        }
    }
        
     var dyVideoTitle: Driver<String?> {
         return trillEntityDriver.map { (trillEntity) -> String? in
             return trillEntity.dyVideoTitle
             }.asDriver()
     }
    
     var itemShortTitle: Driver<String?> {
        return trillEntityDriver.map { (trillEntity) -> String? in
            return trillEntity.itemshorttitle
            }.asDriver()
    }
    
     var couponMoney: Observable<String?> {
           return trillEntityObserver.map { (trillEntity) -> String? in
            return "券￥\(trillEntity.couponmoney ?? "0")"
           }
       }
    
     var itemPic: Observable<String?> {
        return trillEntityObserver.map { (trillEntity) -> String? in
            return trillEntity.itempic ?? ""
        }
    }
    
    var shopType: Observable<String?> {
                 return trillEntityObserver.map { (trillEntity) -> String? in
                    return trillEntity.shoptype
                 }
             }
    
     var itemEndPrice: Observable<String?> {
              return trillEntityObserver.map { (trillEntity) -> String? in
                 return trillEntity.itemendprice
              }
          }
    
    var itemPrice: Observable<String?> {
        return trillEntityObserver.map { (trillEntity) -> String? in
           return trillEntity.itemprice
        }
    }
    
     var itemPrice1: Observable<String?> {
           return trillEntityObserver.map { (trillEntity) -> String? in
              return trillEntity.tkmoney
           }
       }
    
     var itemPrice2: Observable<String?> {
        return trillEntityObserver.map { (trillEntity) -> String? in
           return trillEntity.tkmoney
        }
    }
    
}

//MARK: - 逻辑操作
extension VideoCellViewModel {
    //点赞
    func postLikedStatus(){
        var tempNum = 0
        isLikedStatus.accept(!isLikedStatus.value)
        dyVideoLikeCount.asObservable().subscribe(onNext:{ count in
            tempNum = count
            }).disposed(by:bag)
        if isLikedStatus.value {
           tempNum += 1
        }else{
            tempNum -= 1
        }
        self.dyVideoLikeCount.accept(tempNum)
    }
    
    //双击点赞
    func doubleClickLikedStatus(){
        var tempNum:Int = dyVideoLikeCount.value
       
        if !isLikedStatus.value {
            tempNum += 1
            self.dyVideoLikeCount.accept(tempNum)
        }
        isLikedStatus.accept(true)
        print("双击点赞",isLikedStatus.value)
    }
    
    //收藏
    func postCollectionStatus(){
        isCollectionStatus.accept(!isCollectionStatus.value)
    }
    
}
